package chain

import "github.com/simplechain-org/go-simplechain/core/types"

type Header struct {
	ParentHash      string `json:"parentHash"`
	UncleHash       string `json:"sha3Uncles"`
	Coinbase        string `json:"miner"`
	Root            string `json:"stateRoot"`
	TxHash          string `json:"transactionsRoot"`
	ReceiptHash     string `json:"receiptsRoot"`
	Bloom           string `json:"logsBloom"`
	Difficulty      string `json:"difficulty"`
	Number          string `json:"number"`
	GasLimit        string `json:"gasLimit"`
	GasUsed         string `json:"gasUsed"`
	Time            string `json:"timestamp"`
	Extra           string `json:"extraData"`
	MixDigest       string `json:"mixHash"`
	Nonce           string `json:"nonce"`
	TotalDifficulty string `json:"totalDifficulty"`
	Size            string `json:"size"`
	Hash            string `json:"hash"`
}

// GetHeaderByNumber 获取区块头
func (c *Client) GetHeaderByNumber(number string) (*types.Header, error) {
	var header types.Header
	err := c.rpcClient.Call(&header, "eth_getHeaderByNumber", number)
	if err != nil {
		return nil, err
	}
	return &header, nil
}
func ExchangeBlockHeader(inputBlock interface{}) error {
	switch inputBlock.(type) {
	case *Block:
		block := inputBlock.(*Block)
		tmp, err := HexStringToDecString(block.GasLimit)
		if err != nil {
			return err
		}
		block.GasLimit = *tmp
		tmp, err = HexStringToDecString(block.Difficulty)
		if err != nil {
			return err
		}
		block.Difficulty = *tmp

		tmp, err = HexStringToDecString(block.GasUsed)
		if err != nil {
			return err
		}
		block.GasUsed = *tmp

		tmp, err = HexStringToDecString(block.Nonce)
		if err != nil {
			return err
		}
		block.Nonce = *tmp

		tmp, err = HexStringToDecString(block.Number)
		if err != nil {
			return err
		}
		block.Number = *tmp

		tmp, err = HexStringToDecString(block.Size)
		if err != nil {
			return err
		}
		block.Size = *tmp

		tmp, err = HexStringToDecString(block.Time)
		if err != nil {
			return err
		}
		block.Time = *tmp

		tmp, err = HexStringToDecString(block.TotalDifficulty)
		if err != nil {
			return err
		}
		block.TotalDifficulty = *tmp
	case *MixedBlock:
		block := inputBlock.(*MixedBlock)
		tmp, err := HexStringToDecString(block.GasLimit)
		if err != nil {
			return err
		}
		block.GasLimit = *tmp
		tmp, err = HexStringToDecString(block.Difficulty)
		if err != nil {
			return err
		}
		block.Difficulty = *tmp

		tmp, err = HexStringToDecString(block.GasUsed)
		if err != nil {
			return err
		}
		block.GasUsed = *tmp

		tmp, err = HexStringToDecString(block.Nonce)
		if err != nil {
			return err
		}
		block.Nonce = *tmp

		tmp, err = HexStringToDecString(block.Number)
		if err != nil {
			return err
		}
		block.Number = *tmp

		tmp, err = HexStringToDecString(block.Size)
		if err != nil {
			return err
		}
		block.Size = *tmp

		tmp, err = HexStringToDecString(block.Time)
		if err != nil {
			return err
		}
		block.Time = *tmp

		tmp, err = HexStringToDecString(block.TotalDifficulty)
		if err != nil {
			return err
		}
		block.TotalDifficulty = *tmp
	}

	return nil
}
func ExchangeUncleHeader(header *Header) error {
	tmp, err := HexStringToDecString(header.GasLimit)
	if err != nil {
		return err
	}
	header.GasLimit = *tmp
	tmp, err = HexStringToDecString(header.Difficulty)
	if err != nil {
		return err
	}
	header.Difficulty = *tmp

	tmp, err = HexStringToDecString(header.GasUsed)
	if err != nil {
		return err
	}
	header.GasUsed = *tmp

	tmp, err = HexStringToDecString(header.Nonce)
	if err != nil {
		return err
	}
	header.Nonce = *tmp

	tmp, err = HexStringToDecString(header.Number)
	if err != nil {
		return err
	}
	header.Number = *tmp

	tmp, err = HexStringToDecString(header.Size)
	if err != nil {
		return err
	}
	header.Size = *tmp

	tmp, err = HexStringToDecString(header.Time)
	if err != nil {
		return err
	}
	header.Time = *tmp

	tmp, err = HexStringToDecString(header.TotalDifficulty)
	if err != nil {
		return err
	}
	header.TotalDifficulty = *tmp
	return nil
}
